package com.ysstech.schedule.controller;


import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.schedule.entity.ScheduleLog;
import com.ysstech.schedule.service.ScheduleLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2020-09-21
 */
@Slf4j
@Api(tags = "定时任务日志管理api")
@Controller
@RequestMapping("/scheduleLog")
public class ScheduleLogController extends BaseController {

    @Autowired
    private ScheduleLogService scheduleLogService;

    @YssPermissions(value = {"schedule:logger"})
    @YssLog(module = "定时任务管理中心", remark = "跳转到定时任务日志页面")
    @ApiOperation(value = "跳转到定时任务日志页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "schedule/logger";
    }

    @YssPermissions(value = {"schedule:logger"})
    @YssLog(module = "定时任务管理中心", remark = "查询定时任务日志")
    @ApiOperation(value = "查询定时任务日志")
    @RequestMapping(value = "/listScheduleLog")
    @ResponseBody
    public RestResult listScheduleLog(ScheduleLog scheduleLog, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("查询定时任务日志成功", scheduleLogService.listScheduleLog(scheduleLog, this.getPage(page, rows)));
    }

    @YssPermissions(value = {"schedule:logger:del"})
    @YssLog(module = "定时任务管理中心", remark = "删除定时任务日志")
    @ApiOperation(value = "删除定时任务日志")
    @RequestMapping(value = "/deleteScheduleJobLog")
    @ResponseBody
    public RestResult deleteScheduleJobLog(String arrlogId) throws Exception {
        if (StringUtils.isBlank(arrlogId)) {
            throw new BusinessException("获取删除定时任务信息失败，请稍后重试！");
        }
        List<String> logIds = JSONArray.parseArray(arrlogId, String.class);
        scheduleLogService.removeByIds(logIds);
        return ResultGenerator.getSuccessResult("删除定时任务日志成功");
    }

}
